Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
Python - Variable declarations
Conditional Statements in Python
Loops in Python
Python Functions
Python Classes
OOPS in Python
Python - Hello World Application
Python - import statements
Python - Web API

OOPS in Python



Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which contain data and methods. Here are the core OOP concepts in Python:

  1. Classes and Objects

    • A class is a blueprint for creating objects. It defines a set of attributes and methods that the created objects will have.

    • An object is an instance of a class. It can have its own unique values for the attributes defined in the class.

    python
    class Car:
        def __init__(self, make, model):
            self.make = make
            self.model = model
    
    my_car = Car("Tesla", "Model S")
    
  2. Encapsulation

    • Encapsulation is the concept of bundling the data (attributes) and methods that operate on the data into a single unit, or class, and restricting access to some of the object's components.

    • This is achieved using private (underscore-prefixed) and public attributes/methods.

    python
    class Account:
        def __init__(self, owner, balance=0):
            self.owner = owner
            self.__balance = balance
    
        def deposit(self, amount):
            self.__balance += amount
    
        def __secret_method(self):
            pass  # this is a "private" method
    
  3. Inheritance

    • Inheritance allows a class to inherit attributes and methods from another class, which promotes code reuse.

    • The class that inherits is called the child or subclass, and the class being inherited from is the parent or superclass.

    python
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    class Student(Person):
        def __init__(self, name, age, student_id):
            super().__init__(name, age)
            self.student_id = student_id
    
  4. Polymorphism

    • Polymorphism allows different classes to be treated as instances of the same class through a common interface. It supports method overriding and method overloading.

    • Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

    python
    class Animal:
        def speak(self):
            raise NotImplementedError("Subclass must implement abstract method")
    
    class Dog(Animal):
        def speak(self):
            return "Woof!"
    
    class Cat(Animal):
        def speak(self):
            return "Meow!"
    
    def animal_speak(animal):
        print(animal.speak())
    
    my_dog = Dog()
    my_cat = Cat()
    animal_speak(my_dog)  # Outputs: Woof!
    animal_speak(my_cat)  # Outputs: Meow!
    
  5. Abstraction

    • Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object.

    • This can be achieved through abstract classes and interfaces in Python.

    python
    from abc import ABC, abstractmethod
    
    class Vehicle(ABC):
        @abstractmethod
        def start_engine(self):
            pass
    
    class Car(Vehicle):
        def start_engine(self):
            print("Engine started")
    
    my_car = Car()
    my_car.start_engine()  # Outputs: Engine started
    

These are the fundamental concepts of OOP in Python! If you have any specific questions or need further explanations, feel free to ask.




All rights reserved | Privacy Policy | Sitemap